﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Collections;
using System.IO;
using QuanLyNhapXuat.Presentation.Report;

namespace QuanLyNhapXuat.Presentation.WinForm
{
    public partial class frmThongKeChiPhi : Telerik.WinControls.UI.RadForm
    {
        private static frmThongKeChiPhi doituong = null;
        public static frmThongKeChiPhi TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frmThongKeChiPhi();
            return doituong;
        }
        public frmThongKeChiPhi()
        {
            InitializeComponent();
        }

        private DataSet ds_cp = null;
        private DataSet ds_nam = null;
        //them service so thu chi
        SR_SoThuChi.WS_SoThuChiSoapClient sr_cp = new QuanLyNhapXuat.SR_SoThuChi.WS_SoThuChiSoapClient();
        private void frmThongKeChiPhi_Load(object sender, EventArgs e)
        {
            ds_cp = new DataSet();
            ds_nam = new DataSet();
            //load nam vao drop down list nam
            string cautruyvan = "select distinct YEAR(ngaychi) as 'nam' from sothuchi";
            ds_nam.ReadXml(new StringReader(sr_cp.SelectSQL(cautruyvan)));
            radDDL_nam.DataSource = ds_nam.Tables[0];
            radDDL_nam.DisplayMember = "nam";
            ddl_nam.DataSource = ds_nam.Tables[0];
            ddl_nam.DisplayMember = "nam";
        }
        private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
        {
            //rd_thang.Checked = !rd_ngay.Checked;
            //rd_Nam.Checked = !rd_ngay.Checked;
            rd_thang.Checked = false;
            rd_Nam.Checked = false;
            if (rd_ngay.Checked == true)
            {
                dateTP_DenNgay.Enabled = true;
                dateTP_TuNgay.Enabled = true;
            }
            else
            {
                dateTP_DenNgay.Enabled = false;
                dateTP_TuNgay.Enabled = false;
            }
        }

        private void rd_thang_CheckedChanged(object sender, EventArgs e)
        {
            //rd_ngay.Checked = !rd_thang.Checked;
            //rd_Nam.Checked = !rd_thang.Checked;
            rd_ngay.Checked = false;
            rd_Nam.Checked = false;
            if (rd_thang.Checked == true)
            {
                ddl_thang.Enabled = true;
                ddl_nam.Enabled = true;
            }
            else
            {
                ddl_thang.Enabled = false;
                ddl_nam.Enabled = false;
            }
        }

        private void btn_ThongKeDuLieu_Click(object sender, EventArgs e)
        {
            if (rd_ngay.Checked == true)
            {
                if (treeview_TongQuat.Nodes.Count != 0)
                    treeview_TongQuat.Nodes.Clear();

                gridview_ChiTietThongKe.DataSource = null;
                int ngaybd=dateTP_TuNgay.Value.Day;
                int ngaykt=dateTP_DenNgay.Value.Day;
                int thangbd=dateTP_TuNgay.Value.Month;
                int thangkt = dateTP_DenNgay.Value.Month;
                int nambd = dateTP_TuNgay.Value.Year;
                int namkt=dateTP_DenNgay.Value.Year;
                string cautruyvan = "select ngaychi,diengiai,sotien from SOTHUCHI where DAY(NGAYCHI) between '" + ngaybd.ToString() + "' and '" + ngaykt.ToString() + "' and MONTH(NGAYCHI) between '" + thangbd.ToString() + "' and '" + thangkt.ToString() + "' and YEAR(ngaychi) between '" + nambd.ToString() + "' and '" + namkt.ToString() + "' order by NGAYCHI desc";
                ds_cp.ReadXml(new StringReader(sr_cp.SelectSQL(cautruyvan)));
                if (ds_cp.Tables.Count != 0)
                {
                    DataTable dt = ds_cp.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i][2].ToString().Contains(".0000") == true)
                        {
                            dt.Rows[i][2] = dt.Rows[i][2].ToString().Replace(".0000", "");
                            dt.Rows[i]["NgayChi"] = DateTime.Parse(dt.Rows[i]["NgayChi"].ToString()).ToShortDateString();
                        }
                    }
                    gridview_ChiTietThongKe.DataSource = dt;
                    btn_XuatBCCT.Enabled = true;
                }
                else
                {
                    gridview_ChiTietThongKe.DataSource = null;
                    MessageBox.Show("Không có dữ liệu thống kê !!!");
                }
            }
            else if(rd_thang.Checked==true)
            {
                if (treeview_TongQuat.Nodes.Count != 0)
                    treeview_TongQuat.Nodes.Clear();

                treeview_TongQuat.Nodes.Add("Tháng " + ddl_thang.Text);
                treeview_TongQuat.Nodes[0].BackColor = Color.Blue;                
                DataSet tam = new DataSet();
                string cautruyvan="select MONTH(NGAYCHI) as 'Thang',tencp,SUM(sotien) as 'Tong So Tien' from SOTHUCHI,CHIPHI where SOTHUCHI.MACP=CHIPHI.MACP and month(ngaychi)='" + ddl_thang.Text + "' and year(ngaychi)='"+ddl_nam.Text+"' group by TENCP,month(NGAYCHI)";
                tam.ReadXml(new StringReader(sr_cp.SelectSQL(cautruyvan)));
                string sotien;
                decimal tongtien = 0;
                if (tam.Tables.Count != 0)
                {
                    for (int j = 0; j < tam.Tables[0].Rows.Count; j++)
                    {
                        sotien = tam.Tables[0].Rows[j][2].ToString();
                        treeview_TongQuat.Nodes[0].Nodes.Add(tam.Tables[0].Rows[j][1].ToString() + ": \a" + sotien.Replace(".0000", "") + " VND");
                        tongtien += decimal.Parse(sotien.Replace(".0000", ""));
                    }
                    treeview_TongQuat.Nodes[0].Nodes.Add("Tổng số tiền:" + tongtien.ToString("#,##0") + " VND");
                    btn_XuatBCTQ.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Không có dữ liệu thống kê !!!");
                }
            }
            else if(rd_Nam.Checked==true)
            {
                if (treeview_TongQuat.Nodes.Count != 0)
                    treeview_TongQuat.Nodes.Clear();

                DataSet ds_thang = new DataSet();                
                string nam=radDDL_nam.Text;
                string tien;
                string cautruyvan = "select distinct month(ngaychi) as 'Thang' from SOTHUCHI where YEAR(ngaychi)='"+nam+"'";
                ds_thang.ReadXml(new StringReader(sr_cp.SelectSQL(cautruyvan)));
                string thang;
                if (ds_thang.Tables.Count != 0)
                {
                    DataTable tam = new DataTable();
                    DataSet ds_tam = new DataSet();
                    tam = ds_thang.Tables[0];
                    for (int i = 0; i < tam.Rows.Count; i++)
                    {
                        treeview_TongQuat.Nodes.Add("Tháng " + tam.Rows[i][0].ToString());
                        treeview_TongQuat.Nodes[i].BackColor = Color.Red;
                        treeview_TongQuat.Nodes[i].BackColor2 = Color.Yellow;
                        thang = treeview_TongQuat.Nodes[i].Value.ToString();
                        decimal tongtien = 0;
                        string sql = "select MONTH(NGAYCHI) as 'Thang',tencp,SUM(sotien) as 'Tong So Tien' from SOTHUCHI,CHIPHI where SOTHUCHI.MACP=CHIPHI.MACP and month(ngaychi)='" + thang.Substring(6, 2).ToString() + "' group by TENCP,month(NGAYCHI)";
                        ds_tam.ReadXml(new StringReader(sr_cp.SelectSQL(sql)));
                        if (ds_tam.Tables.Count != 0)
                        {
                            DataTable dt_tam = new DataTable();
                            dt_tam = ds_tam.Tables[0];
                            for (int j = 0; j < dt_tam.Rows.Count; j++)
                            {
                                tien = dt_tam.Rows[j][2].ToString();
                                treeview_TongQuat.Nodes[i].Nodes.Add(dt_tam.Rows[j][1].ToString() + ":" + tien.Replace(".0000", "") + " VND");
                                tongtien += decimal.Parse(tien);                                
                            }
                            treeview_TongQuat.Nodes[i].Nodes.Add("Tổng số tiền:" + tongtien.ToString("#,##0") + " VND");
                            ds_tam.Clear();
                        }
                    }
                    btn_XuatBCTQ.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Không có dữ liệu thống kê !!!");
                }
            }
        }

        private void treeview_TongQuat_SelectedNodeChanged(object sender, Telerik.WinControls.UI.RadTreeViewEventArgs e)
        {
            string tam;
            int index;
            string tencp;
            DataSet ds_tam = new DataSet();
            DataTable dt_tam = new DataTable();
            string parent;
            try
            {
                tam = e.Node.Value.ToString();
                parent = e.Node.Parent.Value.ToString();
                index = tam.IndexOf(":");
                tencp = tam.Substring(0, index);
                string cautruyvan="select ngaychi,diengiai,sotien from SOTHUCHI,CHIPHI where SOTHUCHI.MACP=CHIPHI.MACP and MONTH(NGAYCHI)='"+parent.Substring(6,2).ToString()+"' and TENCP=N'"+tencp+"'";
                ds_tam.ReadXml(new StringReader(sr_cp.SelectSQL(cautruyvan)));
                if (ds_tam.Tables.Count != 0)
                {
                    dt_tam = ds_tam.Tables[0];
                    for (int i = 0; i < dt_tam.Rows.Count; i++)
                    {
                        if (dt_tam.Rows[i][2].ToString().Contains(".0000") == true)
                        {
                            dt_tam.Rows[i][2] = dt_tam.Rows[i][2].ToString().Replace(".0000", "");
                        }
                    }
                }
                gridview_ChiTietThongKe.DataSource = dt_tam;
                
            }
            catch (Exception ex)
            {
            }
        }

        private void rd_Nam_CheckedChanged(object sender, EventArgs e)
        {
            //rd_ngay.Checked = !rd_Nam.Checked;
            //rd_thang.Checked = !rd_Nam.Checked;
            rd_ngay.Checked = false;
            rd_thang.Checked = false;
            if (rd_Nam.Checked == true)
            {
                radDDL_nam.Enabled = true;
            }
            else
                radDDL_nam.Enabled = false;
        }

        private void btn_XuatBCTQ_Click(object sender, EventArgs e)
        {
            if (rd_thang.Checked == true)
            {
                frm_reportChiPhi_Thang report = new frm_reportChiPhi_Thang();
                report.thang = int.Parse(ddl_thang.Text.ToString());
                report.Show();
            }
            if(rd_Nam.Checked==true)
            {
                frm_reportChiPhi_Nam report_nam = new frm_reportChiPhi_Nam();
                report_nam.nam = int.Parse(ddl_nam.Text.ToString());
                report_nam.Show();
            }
        }

        private void btn_XuatBCCT_Click(object sender, EventArgs e)
        {
            if (gridview_ChiTietThongKe.Rows.Count > -1)
            {
                frm_reportChiPhi_Ngay report = new frm_reportChiPhi_Ngay();
                report.NgayBD = dateTP_TuNgay.Value;
                report.NgayKT = dateTP_DenNgay.Value;
                report.Show();
            }
        } 
    }
}
